<template>
  <div class="app-container">
    <div class="search-div">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="标签名称">
          <el-input v-model="form.name" style="width: 200px" placeholder="请输入标签名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="标签分类">
          <el-input v-model="form.type" style="width: 200px" placeholder="请输入标签分类" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-div">
      <div class="table-oper-div">
        <el-button type="success" :icon="Plus" @click="openDialog('add')" circle/>
        <el-button type="primary" :icon="Edit" @click="openDialog('edit')" circle/>
        <el-button type="danger" :icon="Delete" @click="deleteLabel" circle/>
      </div>
      <el-table
          ref="multipleTableRef"
          :data="tableData"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          stripe
          border
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column property="name" label="标签名"/>
        <el-table-column property="tag" label="标签标识"/>
        <el-table-column property="type" label="标签分类"/>
        <el-table-column property="pid" label="父标签ID"/>
        <el-table-column property="remark" label="备注"/>
      </el-table>
      <el-pagination :page-size="form.pageSize" v-model:current-page="form.pageNum"
                     @current-change="handleCurrentChange" layout="prev, pager, next" v-model:total="form.total"/>
      <!--  新增、编辑弹窗 -->
      <el-dialog
          v-model="dialogVisible"
          :title="dialogTitle"
          width="600"
          :destroy-on-close="true"
      >
        <label-edit :form="editForm" @close="dialogVisible = false" @save="saveCallBack"></label-edit>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import {deleteLabelApi, getLabelPageApi} from "@/assets/api/system/label";
import {Delete, Edit, Plus} from "@element-plus/icons-vue";
import {ElMessage, ElMessageBox} from "element-plus";
import LabelEdit from "@/components/view/system/label/labelEdit.vue";

// 搜索条件
const form = ref({
  name: '',
  type: '',
  pageNum: 1,
  pageSize: 10,
  total: 0
})

const onSubmit = () => {
  getTableData();
}

const onReset = () => {
  form.name = '';
  form.type = '';
}

// 全选按钮
const multipleTableRef = ref()
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

// 获取用户列表
const tableData = ref([]);
const getTableData = () => {
  getLabelPageApi(form.value).then(res => {
    tableData.value = res.data.data.records;
    form.value.total = res.data.data.total;
  });
}

// 页码切换处理器
const handleCurrentChange = () => {
  getTableData();
}

// 新增、编辑弹窗
const dialogVisible = ref(false)
const dialogTitle = ref('')
const editForm = ref({}); // 编辑的数据
const openDialog = (tag) => {
  if (tag === 'edit' && multipleSelection.value.length !== 1) {
    ElMessage({
      message: '请选择一条数据进行编辑',
      type: 'warning'
    });
    return;
  }
  dialogTitle.value = tag === 'add' ? '新增用户' : '编辑用户';
  if (tag === 'edit') {
    editForm.value = JSON.parse(JSON.stringify(multipleSelection.value[0]));
    console.log(editForm.value)
  } else {
    editForm.value = {};
  }
  dialogVisible.value = true;
}

// 编辑修改回调
const saveCallBack = (saved) => {
  if (saved) {
    dialogVisible.value = false;
    getTableData();
  }
}

// 删除
const deleteLabel = () => {
  if (multipleSelection.value.length === 0) {
    ElMessage({
      message: '请选择一条数据进行删除',
      type: 'warning'
    });
    return;
  }
  ElMessageBox.confirm(
      '确认要删除吗？',
      '重要提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    // 将对象中的id取出组成新的数组，然后使用join拼接
    const ids = multipleSelection.value.map(item => item.id);
    deleteLabelApi(ids.join(",")).then(res => {
      if (res.data.code === 200) {
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        getTableData();
      } else {
        ElMessage({
          type: 'error',
          message: res.data.msg,
        })
      }
    })
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消删除',
    })
  })
}

onMounted(() => {
  getTableData();
})

</script>

<style scoped>
.app-container {
  padding: 10px;
}

.table-oper-div {
  padding-left: 15px;
  padding-bottom: 10px;
}
</style>